package com.wu.mvp.app;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import java.util.Locale;


public class APP extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        updateLanguage();
    }

    public static Context getContext(){
        return context;
    }

    public static void updateLanguage() {
        Resources res = context.getResources();
        Configuration config = res.getConfiguration();
        DisplayMetrics metrics = res.getDisplayMetrics();
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        if (locale.getLanguage().equals("zh")) {
            config.locale = Locale.SIMPLIFIED_CHINESE;
            res.updateConfiguration(config, metrics);
        }
        else if(locale.getLanguage().equals("en"))
        {
            config.locale = Locale.US;
            res.updateConfiguration(config, metrics);
        }
    }
}
